package semantic_compute.relation_extraction.yago_matcher;

import common.dao.orient.OrientJDBCDao;
import knowlwdge_graph.knowledge_etl.yago_etl.LoderOrientDB;
import knowlwdge_graph.knowledge_etl.yago_etl.RowYagoTriple;
import knowlwdge_graph.knowledge_etl.yago_etl.SchemaDefinition;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.Statement;

/**
 * Created by julianzliu on 4/23/2017.
 */
public class LoderYagoRelation extends LoderOrientDB {

    static final org.slf4j.Logger logger = LoggerFactory.getLogger(LoderYagoRelation.class);

    private OrientJDBCDao orientJDBCDao;
    private Connection conn;


    public LoderYagoRelation(){
        this.orientJDBCDao = new OrientJDBCDao();
        this.conn = this.orientJDBCDao.getConn();
    }

    public void saveRelatonLabelTripleTuple(RowYagoTriple triple) {
        String subject = triple.subject;
        String predicate = triple.predicate;
        String object = triple.object;
        String tripleid = triple.id;
        Float value = triple.value;

        String subject_rid = getVertexRIDByID(subject);
        String object_id = getVertexRIDByID(object);

        if(object_id == null){
            createRelationLabelVertex(object);
            object_id = getVertexRIDByID(object);
        }
        if(subject_rid == null){
            createVertex(subject);
            subject_rid = getVertexRIDByID(subject);
        }
        createEdge(tripleid, predicate, subject_rid, object_id, value);

    }

    public void saveRelatonGlossTripleTuple(RowYagoTriple triple) {
        String subject = triple.subject;
        String predicate = triple.predicate;
        String object = triple.object;
        String tripleid = triple.id;
        Float value = triple.value;

        String subject_rid = getVertexRIDByID(subject);
        String object_id = getRIDByID(YagoRelationSchema.YAGO_RELATION_GLOSS_CLASS.CLASS_NAME, object);;


        if(object_id == null){
            createRelationGlossVertex(object);
            object_id = getVertexRIDByID(object);
        }
        if(subject_rid == null){
            createVertex(subject);
            subject_rid = getVertexRIDByID(subject);
        }
        createGlossEdge(tripleid, predicate, subject_rid, object_id, value);

    }




    public boolean createRelationLabelVertex(String yago_id){
        String sql = String.format("CREATE VERTEX %s SET %s='%s', %s='%s', %s='%s'",
                YagoRelationSchema.YAGO_RELATION_LABEL_CLASS.CLASS_NAME,
                YagoRelationSchema.YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_ID, yago_id,
                YagoRelationSchema.YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_LABEL, yago_id,
                YagoRelationSchema.YAGO_RELATION_LABEL_CLASS.PROPERTY_YAGO_TYPE, SchemaDefinition.YAGO_NODE_TYPE.TYPE_RELATION);

        logger.debug("[createRelationLabelVertex] sql = {}", sql );

        Statement stmt = null;
        boolean success = true;
        try {
            stmt = this.conn.createStatement();
            success = stmt.execute(sql);
            stmt.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return success;
    }

    public boolean createRelationGlossVertex(String yago_id){
        String sql = String.format("CREATE VERTEX %s SET %s='%s', %s='%s', %s='%s'",
                YagoRelationSchema.YAGO_RELATION_GLOSS_CLASS.CLASS_NAME,
                YagoRelationSchema.YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_ID, yago_id,
                YagoRelationSchema.YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_LABEL, yago_id,
                YagoRelationSchema.YAGO_RELATION_GLOSS_CLASS.PROPERTY_YAGO_TYPE, SchemaDefinition.YAGO_NODE_TYPE.TYPE_RELATION);

        logger.info("[createRelationGLOSSVertex] sql = {}", sql );

        Statement stmt = null;
        boolean success = true;
        try {
            stmt = this.conn.createStatement();
            success = stmt.execute(sql);
            stmt.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return success;
    }

    public boolean createGlossEdge(String yago_id, String yago_label, String head_rid, String tail_rid, Float value){
        String sql = String.format("CREATE EDGE %s FROM %s TO %s SET yago_id = '%s', yago_label = '%s', value = %f",
                YagoRelationSchema.YAGO_RELATION_GLOSS_EDGE.EDGE_NAME,
                head_rid, tail_rid, yago_id, yago_label, value);
        logger.info("[createGlossEdge] sql = {}", sql );

        Statement stmt = null;
        boolean success = true;
        try {
            stmt = this.conn.createStatement();
            success = stmt.execute(sql);
            stmt.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return success;

    }

}




